package cn.kawa.user.service.impl;


import cn.kawa.basic.exception.BusinessException;
import cn.kawa.basic.service.impl.BaseServiceImpl;

import cn.kawa.basic.util.Md5Utils;
import cn.kawa.basic.util.StrUtils;
import cn.kawa.org.domain.Employee;
import cn.kawa.user.domain.LoginInfo;
import cn.kawa.user.domain.User;
import cn.kawa.user.dto.UserDto;
import cn.kawa.user.mapper.LoginInfoMapper;
import cn.kawa.user.mapper.UserMapper;
import cn.kawa.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void phoneRegister(UserDto userDto) {


        //1.校验 - 空值校验 userDto：电话，短信验证码，密码，重复密码
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();
        if(StringUtils.isEmpty(phone) ||StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(phone) ||StringUtils.isEmpty(phone)
        ){
            throw new BusinessException("必填项不能为空~");
        }
        //2.校验 - 密码一致校验 为何用trim（）:删除前后空格
        if(!password.trim().equals(passwordRepeat.trim())){
            throw new BusinessException("两次密码输入不一致~");
        }

        //3.校验 - 验证码是否过期 :[register:131564646 - 4399:12346464611]
        Object redisPhoneCode = redisTemplate.opsForValue().get("register:" + phone);
        if(redisPhoneCode==null){//如果从redisTemplate取不到值
            throw new BusinessException("手机验证码失效~");
        }
        //4.校验 - 验证码校验 :   [0] 可以取到冒号前内容
        if(!redisPhoneCode.toString().split(":")[0].equalsIgnoreCase(phoneCode)){
            throw new BusinessException("手机验证码错误~");
        }

        //5.添加 - loginInfo表 :userDto：电话，短信验证码，密码，重复密码
        User user = userDto2User(userDto);
        LoginInfo loginInfo =user2Logininfo(user);
        loginInfoMapper.save(loginInfo);
        //6.添加 - user表同步添加
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);

    }

    private LoginInfo user2Logininfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo);
        loginInfo.setType(1);//设置登录属性为用户
        return loginInfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setEmail(null);
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String password = Md5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(password);
        user.setState(1);
        //user.setAge();
        //user.setHeadImg();
        //user.setLogininfo_id();
        return user;

    }

    @Override
    public void add(User user) {
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5pwd = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setPassword(md5pwd);
        LoginInfo loginInfo =user2logininfo(user);
        loginInfoMapper.save(loginInfo);
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);
    }

    @Transactional
    @Override
    public void delete(Long id) {

        loginInfoMapper.remove(userMapper.loadById(id).getLogininfo_id());
        userMapper.remove(id);
    }

    private LoginInfo user2logininfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo);
        loginInfo.setType(1);
        return loginInfo;
    }

    //1.loadAll();
    //2.loadById
    //3.save
    //4.remove
    //5.update
    //6.return new PageList<>(count,shops);
    //7.patchDel
}
